<?php
namespace app\mobile\controller;

use app\common\controller\Frontend;
use app\common\model\Category;
use think\Cache;

class News extends Frontend{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';
    public $cacha_time=86400;



    public function index(){

        $Category=new Category();
        $category=$Category->getCategoryArray();
        $assign['category']=$category;

        $mod=model('App');
        $cate=$mod->getCateNameList();
        $this->assign('cate',$cate);

        $list=db('news')
            ->where("status","1")
            ->field(["id","cate_name","title","keywords","description","create_time"])
            ->order("create_time","desc")
            ->cache($this->cacha_time)
            ->paginate(30);

        $list_arr=$list->all();
        foreach ($list_arr as $k=>$v){
            $v["detail_url"]="/news/detail/id/{$v['id']}.html";
            $list[$k]=$v;
        }
        $list_total=$list->total();
        $assign["list_total"]=$list_total;

        $page=$list->render();
        $assign['list']=$list;
        $assign['page']=$page;



        $this->assign($assign);
        return $this->view->fetch("news/list");
    }


    public function detail(){
        $Category=new Category();
        $category=$Category->getCategoryArray();
        $assign['category']=$category;

        //Cache::clear();
        $mod=model('App');
        $cate=$mod->getCateNameList();
        $assign['cate']=$cate;

        //根据ID,查询新闻内容
        $id=(int)request()->param("id");
        if(strlen($id)<1){
            $this->jump404();//跳转404
        }
        $info=db('news')
            ->where(["id"=>$id])
            ->cache($this->cacha_time)
            ->find();
        if(empty($info)){
            $this->jump404();//跳转404
        }
        $str=$info['content'];
        $gzip_content=base64_decode($str);
        $json_content=gzuncompress($gzip_content);
        $str_content=json_decode($json_content,true);
        $info['content']=$str_content;
        $assign['info']=$info;



        //新闻推荐列表
        $assign["news_tj_list"]=$this->get_news_tj_list();

        //app推荐列表
        $assign['app_tj_list']=$this->get_app_tj_list();





        $this->assign($assign);
        return $this->view->fetch("news/detail");
    }


    //获取每个栏目下的新闻10条最近数据
    private function get_news_tj_list()
    {
        $cache_name="news_mobile_tj_list";
        cache::rm($cache_name);
        if(Cache::has($cache_name)){
            $news_cache_list=Cache::get($cache_name);
        }else{

            $news_cache_list=db("news")
                ->where("status","1")
                ->field(["title","id","create_time"])
                ->order("id","desc")
                ->limit(0,60)
                ->select();
            //设置缓存
            Cache::set($cache_name,$news_cache_list,$this->cacha_time);

        }

        return $news_cache_list;

    }

    //获取APP每个栏目下的最新10条数据
    private function get_app_tj_list(){

        $cache_name="app_mobile_tj_list";
        Cache::rm($cache_name);
        if(Cache::has($cache_name)){
            $app_tj_list=Cache::get($cache_name);
        }else{

            $app_tj_list=db("app")
                ->where("status","1")
                ->field(["id","cate_name","type_name","app_name","img","keywords","create_time"])
                ->order("id","desc")
                ->limit(0,60)
                ->select();
            //设置缓存
            //var_dump($app_tj_list);die();
            Cache::set($cache_name,$app_tj_list,$this->cacha_time);

        }

        return $app_tj_list;
    }

    public function jump_404()
    {
        $this->error("暂无内容!");
    }





}